package com.qyhl.webtv.module_microvideo.shortvideo.search;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoSearchBean;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.module_microvideo.common.VideoUrl;

import java.util.List;

public class ShortVideoSearchModel implements ShortVideoSearchContract.ShortVideoSearchModel {
    private ShortVideoSearchPresenter mPresenter;

    ShortVideoSearchModel(ShortVideoSearchPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getHotTheme(String actId) {

        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_THEME_HOT_LIST)
                .params("actId", actId)
                .execute(new SimpleCallBack<List<ShortVideoThemeBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.setHotError("sv_error:" + e.toString());
                    }

                    @Override
                    public void onSuccess(List<ShortVideoThemeBean> shortVideoThemeBeans) {
                        mPresenter.setHotTheme(shortVideoThemeBeans);
                    }
                });

    }

    @Override
    public void goSearch(String actId, String key, final String type) {
        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_SEARCH_LIST)
                .params("actId", actId)
                .params("key", key)
                .params("type", type)
                .execute(new SimpleCallBack<ShortVideoSearchBean>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(ShortVideoSearchBean shortVideoSearchBean) {
                        switch (type) {
                            case "tag":
                                if (shortVideoSearchBean.getTags() != null && shortVideoSearchBean.getTags().size() > 0) {
                                    mPresenter.setSearch(shortVideoSearchBean);
                                } else {
                                    mPresenter.setSearchError("暂无任何相关内容！");
                                }
                                break;
                            case "works":
                                if (shortVideoSearchBean.getWorks() != null && shortVideoSearchBean.getWorks().size() > 0) {
                                    mPresenter.setSearch(shortVideoSearchBean);
                                } else {
                                    mPresenter.setSearchError("暂无任何相关内容！");
                                }
                                break;
                            case "all":
                                if ((shortVideoSearchBean.getWorks() != null && shortVideoSearchBean.getWorks().size() > 0) || (shortVideoSearchBean.getTags() != null && shortVideoSearchBean.getTags().size() > 0)) {
                                    mPresenter.setSearch(shortVideoSearchBean);
                                } else {
                                    mPresenter.setSearchError("暂无任何相关内容！");
                                }
                                break;
                                default:
                                    break;
                        }
                    }
                });

    }
}
